<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-24
 * Time: 上午10:42
 */
namespace Service\Controller;
use Mcit\Page;

//计费管理
class BillController extends BaseController{

    public function index(){

        ini_set("max_execution_time", "1800");
        $re            = strsToArray(session('location'));
        $location      = $re[0].",".$re[1];
        $user_pay_card = I('user_pay_card');
        if(!empty($_GET['user_pay_card'])){
            $sql       = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> ''
            and a.location like '%$location%'
            and c.user_pay_card like '%$user_pay_card%'";
        }elseif(!empty($_GET['end_time'])){
            $strt_time = date("Y-m-d H:s:i",strtotime(I("start_time")));
            $end_time  = date("Y-m-d H:s:i",strtotime(I("end_time")));
            $sql       = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and a.location like '%$location%'
            and c.time >= '$strt_time' and c.time <= '$end_time'";
        }elseif(!empty($_GET['end_time']) && !empty($_GET['user_pay_card'])){
            $strt_time = date("Y-m-d H:s:i",strtotime(I("start_time")));
            $end_time  = date("Y-m-d H:s:i",strtotime(I("end_time")));
            $sql       = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and c.user_pay_card like '%$user_pay_card%'
            and a.location like '%$location%' and c.time >= '$strt_time' and c.time <= '$end_time'";
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $strt_time = I("start_time");
            $sql       = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and a.location like '%$location%' and c.time like '%$strt_time%'";
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == '' && !empty($_GET['user_pay_card'])){
            $strt_time = I("start_time");
            $sql       = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and c.user_pay_card like '%$user_pay_card%'
            and a.location like '%$location%' and c.time like '%$strt_time%'";
        }else{
            $sql       = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and a.location like '%$location%'";
        }
        //分页
        $count = count(M()->query($sql));
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] * 20 - 20 : 0;
        if(!empty($_GET['user_pay_card'])){
            $sql1      = "select a.name,a.location,c.id,c.user_pay_card,c.start_time,c.end_time,c.this_reading,c.this_amount
            from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and a.location like '%$location%'
            and c.user_pay_card like '%$user_pay_card%'
            limit $nowPage,".$Page->listRows;
        }elseif(!empty($_GET['end_time']) && !empty($_GET['user_pay_card'])){
            $strt_time = date("Y-m-d H:s:i",strtotime(I("start_time")));
            $end_time  = date("Y-m-d H:s:i",strtotime(I("end_time")));
            $sql1      = "select a.name,a.location,c.id,c.user_pay_card,c.start_time,c.end_time,c.this_reading,c.this_amount
            from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and c.user_pay_card like '%$user_pay_card%'
            and a.location like '%$location%' and c.time >= '$strt_time'
            and c.time <= '$end_time'
            limit $nowPage,".$Page->listRows;
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == '' && !empty($_GET['user_pay_card'])){
            $strt_time = I("start_time");
            $sql1      = "select a.name,a.location,c.id,c.user_pay_card,c.start_time,c.end_time,c.this_reading,c.this_amount
            from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and c.user_pay_card like '%$user_pay_card%'
            and a.location like '%$location%' and c.time like '%$strt_time%'
            limit $nowPage,".$Page->listRows;
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $strt_time = I("start_time");
            $sql1      = "select a.name,a.location,c.id,c.user_pay_card,c.start_time,c.end_time,c.this_reading,c.this_amount
            from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and a.location like '%$location%'
            and c.time like '%$strt_time%'
            limit $nowPage,".$Page->listRows;
        }else{
            $sql1      = "select a.name,a.location,c.id,c.user_pay_card,c.start_time,c.end_time,c.this_reading,c.this_amount
            from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where c.user_pay_card <> '' and a.location like '%$location%'
            limit $nowPage,".$Page->listRows;
        }
        $pay = M()->query($sql1);
//        echo M()->getLastSql();
        //分页结束
        foreach($pay as $k=>$v){
            if(mb_strlen($v['name'],'utf-8') <= 17){
                $pay[$k]['plant_name'] = $v['name'];
            }else{
                $pay[$k]['plant_name'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
            $region_arr = strsToArray($v['location']);//查所在地
            $region     = D("Admin/Region")->where("code='".$region_arr[2]."'")->getField('fullname');
            $pay[$k]['location_text'] = $region;
            $pay[$k]['plant_text']    = $v['name'];
            $pay[$k]['time_num']      = round((strtotime($v['end_time']) - strtotime($v['start_time'])) / 3600,2);
        }
//        P($pay);
        $this->assign('pay',$pay);
        $this->display();
    }
    //查看当前详细信息
    public function detail(){

        if(IS_GET){
            $data   = D("Admin/Pay")->where("id='".I('id')."'")->find();
            $sql    = "select c.name,c.location,c.address from char_pay_record a
            inner join ev_char b on ".$data['key_link']."=b.key_link inner join ev_plant c on b.ev_plant_id=c.id";
            $plants = M()->query($sql);
            foreach($plants as $v){
                $data['name'] = $v['name'];
                $data['address'] = $v['address'];
                $region_arr = strsToArray($v['location']);//查所在地
                $region = D("Admin/Region")->where("code='".$region_arr[2]."'")->getField('fullname');
                $data['location'] = $region;
            }
            //P($data);
            $this->data = $data;
        }
        $this->display();
    }
}
